package hottargui.domain.gamma;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import hottargui.domain.*;
import hottargui.domain.standard.*;

public class GammaDeckOfCardsImpl implements DeckOfCards{

	private List<Card> _cards;
	private int _diceValue;  
	private ShuffleStrategy _shuffleStrategy; 

	public int countCardsInDeck() {
		return _cards.size();
	}




	//Constructing a deck with 4 cards - buy sequense. Is not random until Shuffeled
	public GammaDeckOfCardsImpl(ShuffleStrategy shuffleStrategy) {
		_cards = new ArrayList<Card>();
		createCardsOnDeck(1);
		_diceValue=1;
		_shuffleStrategy = shuffleStrategy;
	}


	//Constructing a Deck from StrategyImplementation
	public GammaDeckOfCardsImpl(CollectionOfCardsStrategy deck, ShuffleStrategy shuffleStrategy) {
		_cards = deck.getCards();
		_diceValue=deck.getDiceValue();
		_shuffleStrategy = shuffleStrategy;
	}




	// 	Construck a af deck of cards on the basis of a dicevalue
	// and according to game rules. The deck is not random until it is shuffled
	public GammaDeckOfCardsImpl(int diceValue, ShuffleStrategy shuffleStrategy) {
		_cards = new ArrayList<Card>();
		createCardsOnDeck(diceValue);
		_diceValue = diceValue;
		_shuffleStrategy = shuffleStrategy;

	}

	/** puts one card on top of the deck 
	 */
	public void putCardOnDeck(Card card) {
		_cards.add(card);
	}


	/** return the Tribecard on top of deck and delete it from stack 
	 * @return a Card from the deck.
	 */
	public Card takeCardFormTopOfDeck() {

		int size = 0;
		int lastElement = 0;
		if (_cards.isEmpty()) 
			return null;
		else {
			size = _cards.size();
			lastElement = size-1;
			Card onTop = _cards.get(lastElement);
			_cards.remove(lastElement);
			return onTop;
		}



	}


	/**   
	 * This method randomizes the deck.
	 */
	public void shuffle() {
		_cards = _shuffleStrategy.shuffle(_cards); 
		//Card c = new StandardCardImpl(PlayerColor.None); 
		//_cards.add(c);


	}



	public int getNumberOfTribeCards(PlayerColor playerColor) {


		ListIterator<Card> li = _cards.listIterator();
		Card card = null;
		int c = 0;

		while (li.hasNext()) {
			card = li.next();
			if (card.getPlayerColor()==playerColor && card.getCardType() == CardType.Tribe)
				c++;
		}	

		return c;

	}


	public void printCardsInDeck() {

		Card card = null;
		ListIterator<Card> li = _cards.listIterator();
		while (li.hasNext()) {
			card = li.next();
			System.out.println(card.getCardType()+ " " + card.getPlayerColor());
		}	



	}


	public void createCardsOnDeck(int diceValue) {

		//to be empty
		_cards = new ArrayList<Card>();

		if (diceValue<6) {

			// create and add cards
			for (int i = 0; i < diceValue; i++){
				this.putCardOnDeck(new StandardCardImpl(PlayerColor.Yellow));

			}



			// create and add cards BluePlayer
			for (int i = 0; i < diceValue; i++){
				this.putCardOnDeck(new StandardCardImpl(PlayerColor.Blue));

			}

			// create and add cards GreenPlayer
			for (int i = 0; i < diceValue; i++){
				this.putCardOnDeck(new StandardCardImpl(PlayerColor.Green));

			}


			// create and add cards RedPlayer
			for (int i = 0; i < diceValue; i++){
				this.putCardOnDeck(new StandardCardImpl(PlayerColor.Red));
			}			



		}	

		else {
			this.putCardOnDeck(new StandardCardImpl(PlayerColor.Yellow));
			this.putCardOnDeck(new StandardCardImpl(PlayerColor.Blue));
			this.putCardOnDeck(new StandardCardImpl(PlayerColor.Green));
			this.putCardOnDeck(new StandardCardImpl(PlayerColor.Red));
		}



	}


	public int getCurrentDeckBasedDiceValue() {
		return _diceValue;
	}


}
